package com.deyuanyun.pic.pbf.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.BaseController;
import com.deyuanyun.pic.wf.domain.Node;
import com.deyuanyun.pic.wf.domain.ProcessDefinition;
import com.deyuanyun.pic.wf.domain.ProcessInstance;
import com.deyuanyun.pic.wf.service.ProcessDefinitionService;
import com.deyuanyun.pic.wf.service.RepairWorkflowService;

/**
 *  维修工作流程Controller
 *
 * @author ChenKui
 * @date 2016-06-16
 */
@Controller
@RequestMapping("/pbf/RepairWorkflow")
public class RepairWorkflowController extends BaseController{


    private @Autowired RepairWorkflowService repairWorkflowService;
    private @Autowired ProcessDefinitionService processDefinitionService;


    //初始化进入页面
    @RequestMapping("/init.html")
    public ModelAndView init1(ModelMap model) {
        
        return forword("/pbf/flowManage/flowList", model);
    }
    
    //页面初始化数据
    @RequestMapping("/init.json")
    @ResponseBody
    public void init2(ModelMap model) {

        List<Node> node_list = repairWorkflowService.queryNodeByNeed();
        List<ProcessDefinition> pd_list = processDefinitionService.queryProcessDefinitionPbf();
        List<Map<String,Object>> pi_list = repairWorkflowService.queryProcessInstanceList();
        
        model.addAttribute("node_list", node_list);
        model.addAttribute("pd_list", pd_list);
        model.addAttribute("pi_list", pi_list);
        
        AjaxSupport.sendSuccessText("success", model);
    }

    /*
    @InitBinder("processDefinition")
    protected void initBinder_processDefinition(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("processDefinition.");
    }
    @InitBinder("processInstance")
    protected void initBinder_processInstance(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("processInstance.");
    }*/

    /**
     * 创建或者修改维修流程（在update的情况下，目前不能用于删除节点的情况，实际也无此业务需求）。
     *  参数直接使用的Domain对象。所以需要绝对信任调用者，不然别有用心的人可能会传递诸如processDefinition.deleted=1，只有呵呵呵了。
     *  不过这本身并没有太大的安全性问题，所以就这样了。
     *
     * create调用示例（创建一个名称为“XX修复”，并拥有两个节点的流程）：
     * /pbf/RepairWorkflow/createOrUpdate.json
     *
     * processDefinition.name=XX修复、维修流程名称
     * 
     * processInstance.nodeId={nodeID1}、节点编号
     * processInstance.nodeName={nodeName1}、节点名称
     * processInstance.isPerform={isPerform}、节点是否执行
     *
     * processInstance.nodeId={nodeID2}、节点编号
     * processInstance.nodeName={nodeName2}、节点名称
     * processInstance.isPerform={isPerform}、节点是否执行
     *
     * update调用示例：
     * 在create的基础上增加各自的id即可。
     * processDefinition.id、维修流程编号
     *
     * processInstance.id、维修流程子节点编号
     * processInstance.id、维修流程子节点编号
     *
     * @param processDefinition
     * @param processInstanceList
     */
    @RequestMapping("/createOrUpdate.json")
    @ResponseBody
    /**/
    public void createOrUpdate(
            @RequestParam(value = "processDefinition.name") String pdName
            ,@RequestParam(value = "processDefinition.id",required = false) String pdId
            ,@RequestParam(value = "processInstance.nodeId") String[] nodeIds
            ,@RequestParam(value = "processInstance.nodeName") String[] nodeNames
            ,@RequestParam(value = "processInstance.isPerform") String[] isPerforms
            ,@RequestParam(value = "processInstance.id",required = false) String[] piIds ) {
            
    /*
    public void createOrUpdate(
            @ModelAttribute("processDefinition") ProcessDefinition pd
            ,@ModelAttribute("processInstance") List<ProcessInstance> piList) {
    */
        
        ProcessDefinition pd = new ProcessDefinition();
        pd.setName(pdName);
        if(StringUtils.isNotEmpty(pdId)){
            pd.setId(pdId);
        }
        List<ProcessInstance> piList = new ArrayList<ProcessInstance>(isPerforms.length);
        for (int i = 0; i < nodeIds.length; i++) {
            String nodeId = nodeIds[i];
            String nodeName = nodeNames[i];
            String isPerform = isPerforms[i];
            
            ProcessInstance pi = new ProcessInstance();
            pi.setNodeId(nodeId);
            pi.setNodeName(nodeName);
            pi.setIsPerform(Short.valueOf(isPerform));
            if(piIds != null && piIds.length > 0){
                pi.setId(piIds[i]);
            }
            piList.add(pi);
        }
        repairWorkflowService.createOrUpdate(pd,piList);

        AjaxSupport.sendSuccessText("success", "");

    }


    @RequestMapping("/delete.json")
    @ResponseBody
    public void delete( @RequestParam(value = "pd_id") String pd_id ){

        processDefinitionService.updateToDelete(pd_id);
        AjaxSupport.sendSuccessText("success", "");
    }


}
